package com.meessiom.train.crm.role.service;
import com.meessiom.train.crm.common.exception.ResourceNotFoundException;
import com.meessiom.train.crm.role.model.Role;
import com.meessiom.train.crm.role.repository.RoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class RoleService {
    @Autowired
    private RoleRepository roleRepository;
    public List<Role> getAllRoles(){
        return roleRepository.findAll();
    }

    public Role createrole(Role role){
        return roleRepository.save(role);

    }


    public List<Role> createRoles(List<Role> roles){
        return roleRepository.saveAll(roles);

    }

    public Role getRoleById(long id) {
        Role role = roleRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("role not exist with id" + id));
        return role;
    }

    public String getRoleNameByRoleId(long id){
        String roleName = roleRepository.getRoleNameById(id);
        return roleName;
    }

    public Role updateRole(long id,Role roleDetails) {
        Role updaterole = roleRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("role not exist with id" + id));
        updaterole.setRoleName(roleDetails.getRoleName());
        updaterole.setRoleAddress(roleDetails.getRoleAddress());
        updaterole.setRoleGender(roleDetails.getRoleGender());
        updaterole.setRoleJob(roleDetails.getRoleJob());
//        updaterole.setRoleId(roleDetails.getRoleId());
        roleRepository.save(updaterole);
        return updaterole;
    }
    public void deleteRole(long id) {
        Role  role = roleRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("role not exist with id" + id));

        roleRepository.delete(role);
    }

    public List<Role> createAll(List<Role> roles) {
        return roleRepository.saveAll(roles);
    }
}
